package com.niuwa.p2p.action.promotion;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.core.utils.DateUtil;
import com.niuwa.p2p.entity.gift.PromotionGiftinfo;
import com.niuwa.p2p.entity.promotion.Promotion;
import com.niuwa.p2p.service.gift.GiftService;
import com.niuwa.p2p.service.promotion.PromotionService;

/**
 * 
 * @author zhangchao
 *
 */
@Namespace("/promotion")
@Controller
public class PromotionAction  extends JsonAction{
	private static final long serialVersionUID = 1L;
	
	@Resource
	private PromotionService promotionService;
	
	@Resource
	private GiftService giftService;
	
	private Logger LOGGER = Logger.getLogger(PromotionAction.class);
	
	/**
	 * 跳转到列表界面
	 * @return
	 */
	//@Action("gotoPromotionList")
	@RequestMapping("gotoPromotionList.jsp")
	public String gotoPromotionList(){
		return "promotionlist";
	}
	
	/**
	 * 获取列表数据
	 * @return
	 */
	@Action("getPromotionList")
	public String getPromotionList(){
		setObjectReSult(promotionService.searchPromotionPage(getParamMap()));
		return NONE;
	}
	
	/**
	 * 跳转到新增界面
	 * @return
	 */
	@Action("gotoAddPromotion")
	public String gotoAddPromotion(){
		return "promotiondetail";
	}
	
	/**
	 * 生成活动码
	 * @return
	 */
	@Action("generatPromotionCode")
	public String generatPromotionCode(){
		Map<String, Object> paramMap = getParamMap();
		String promotionForcode = (String)paramMap.get("promotionFor");
		String promotionType = (String)paramMap.get("promotionType");
		Byte promotionTypeInt = new Byte("0");
		if(StringUtils.hasText(promotionType)){
			promotionTypeInt=Byte.parseByte(promotionType);
		}
		String generatPromotionCode = promotionService.generatPromotionCode(promotionForcode, promotionTypeInt);
		setResult(true, "success", generatPromotionCode);
		return NONE;
	}
	@Action("getGiftNotInPromotion")
	public String getGiftNotInPromotion(){
		Map<String, Object> paramMap = getParamMap();
		String promotionId = (String)paramMap.get("promotionId");
		Integer promotionIdInt = new Integer("0");
		if(StringUtils.hasText(promotionId)){
			promotionIdInt=Integer.parseInt(promotionId);
		}
		 List<PromotionGiftinfo> searchGiftNotInPromotion = giftService.searchGiftNotInPromotion(promotionIdInt);
		 setObjectReSult(searchGiftNotInPromotion);
	     return NONE;
	}
	@Action("getGiftInPromotion")
	public String getGiftInPromotion(){
		Map<String, Object> paramMap = getParamMap();
		String promotionId = (String)paramMap.get("promotionId");
		Integer promotionIdInt = new Integer("0");
		if(StringUtils.hasText(promotionId)){
			promotionIdInt=Integer.parseInt(promotionId);
		}
		 List<PromotionGiftinfo> searchGiftInPromotion = giftService.searchGiftInPromotion(promotionIdInt);
		 setObjectReSult(searchGiftInPromotion);
	     return NONE;
	}
	
	@Action("savePromotion")
	public String savePromotion(){
		Map<String, Object> paramMap = getParamMap();
		Promotion promotion = new Promotion();
		String promotionId = (String)paramMap.get("promotionId");
		Integer promotionIdInt = new Integer("0");
		if(StringUtils.hasText(promotionId)){
			promotionIdInt=Integer.parseInt(promotionId);
			promotion.setPromotionId(promotionIdInt);
		}
		String promotionCode =(String)paramMap.get("promotionCode");
		if(StringUtils.hasText(promotionCode)){
			promotion.setPromotionCode(promotionCode);;
		}
		String promotionForcode=(String)paramMap.get("promotionForcode");
		if(StringUtils.hasText(promotionForcode)){
			promotion.setPromotionForcode(promotionForcode);;
		}
		String promotionType=(String)paramMap.get("promotionType");
		if(StringUtils.hasText(promotionType)){
			byte parseByte = Byte.parseByte(promotionType);
			promotion.setPromotionType(parseByte);;
		}
		String promotionName=(String)paramMap.get("promotionName");
		if(StringUtils.hasText(promotionName)){
			promotion.setPromotionName(promotionName);;
		}
		String startTime=(String)paramMap.get("startTime");
		if(StringUtils.hasText(startTime)){
			try {
				promotion.setStartTime(DateUtil.parseStringToDate(startTime, DateUtil.DATE_FORMAT_LONG));
			} catch (Exception e) {
				LOGGER.error(ExceptionUtils.getFullStackTrace(e));
			};
		}
		String endTime=(String)paramMap.get("endTime");
		if(StringUtils.hasText(endTime)){
			try {
				promotion.setEndTime(DateUtil.parseStringToDate(endTime, DateUtil.DATE_FORMAT_LONG));
			} catch (Exception e) {
				LOGGER.error(ExceptionUtils.getFullStackTrace(e));
			};
		}
		 promotionService.savePromotion(promotion);
		 setResult(true, "success", promotion.getPromotionId());
	     return NONE;
	}
	@Action("addGift2Promotion")
	public String addGift2Promotion(){
		Map<String, Object> paramMap = getParamMap();
		String promotionId = (String)paramMap.get("promotionId");
		Integer promotionIdInt = new Integer("0");
		if(StringUtils.hasText(promotionId)){
			promotionIdInt=Integer.parseInt(promotionId);
		}
		 List<PromotionGiftinfo> searchGiftInPromotion = giftService.searchGiftInPromotion(promotionIdInt);
		 setObjectReSult(searchGiftInPromotion);
	     return NONE;
	}
}
